package com.example.shop.common.service.impl;

import com.example.shop.common.dto.mail.AttachFileMail;
import com.example.shop.common.dto.mail.Mail;
import com.example.shop.common.dto.mail.TemplateMail;
import com.example.shop.common.service.MailService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service
public class MailServiceImpl implements MailService {
    /**
     *  springboot 提供的一个发送邮件的简单抽象，直接注入即可
     *  MailSenderPropertiesConfiguration包里配置了实例该bean
     */
    @Resource
    private JavaMailSender mailSender;

    @Override
    public boolean sendSimpleMail(Mail mail) {
        try {
            //创建一个邮箱消息对象
            SimpleMailMessage message = new SimpleMailMessage();
            //配置邮箱发送人
            message.setFrom(mail.getFrom());
            //邮件的收件人
            message.setTo(mail.getTo());
            //邮件的主题
            message.setSubject(mail.getSubject());
            //邮件的内容
            message.setText(mail.getContent());
            //邮件的发送日期
            message.setSentDate(mail.getDate());
            mailSender.send(message);
        }catch (Exception e){
            System.out.println("邮件发送出错");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean sendAttachFileMail(AttachFileMail mail) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            // multipart=true是复合邮件,即可以带附件。
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(mail.getFrom());
            messageHelper.setTo(mail.getTo());
            messageHelper.setSentDate(mail.getDate());
            messageHelper.setSubject(mail.getSubject());
            messageHelper.setText(mail.getContent());
            //会自动帮我们读取文件数据
            List<String> filePaths = mail.getFilePaths();
            for(String filePath:filePaths){
                File file = new File(filePath);
                messageHelper.addAttachment(file.getName(),file);
            }
            mailSender.send(message);
        }catch (Exception e){
            System.out.println("邮件发送出错");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean sendTemplateMail(TemplateMail mail) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            // multipart=true是复合邮件,即可以带附件。
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(mail.getFrom());
            messageHelper.setTo(mail.getTo());
            messageHelper.setSentDate(mail.getDate());
            messageHelper.setSubject(mail.getSubject());
            //设置该信息的文本用html解析
            messageHelper.setText(mail.getContent(),true);
            mailSender.send(message);
        }catch (Exception e){
            System.out.println("邮件发送出错");
            e.printStackTrace();
            return false;
        }
        return true;
    }








}